package com.xf.tourismapp.activities;

import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import androidx.fragment.app.FragmentActivity;
import androidx.fragment.app.FragmentTransaction;
import com.xf.tourismapp.R;
import com.xf.tourismapp.fragments.FoundFragment;
import com.xf.tourismapp.fragments.HomePageFragment;
import com.xf.tourismapp.fragments.MusicFragment;
import com.xf.tourismapp.fragments.MyFragment;

public class MainActivity extends FragmentActivity {
    private RadioButton rb_homePage;
    private RadioButton rb_music;
    private RadioButton rb_found;
    private RadioButton rb_my;
    //定义Fragment对象
    private MyFragment mMyFragment;
    private HomePageFragment mHomePageFragment;
    private MusicFragment mMusicFragment;
    private FoundFragment mFoundFragment;
    private FragmentTransaction ft;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        //设置默认被选中RadioButton导航栏为个人中心“我的”
        setSelection(3);

    }
    private void initView() {
        rb_homePage =findViewById(R.id.rb_homePage);
        rb_music = findViewById(R.id.rb_music);
        rb_found = findViewById(R.id.rb_found);
        rb_my = findViewById(R.id.rb_my);
    }

    /**
     * 点击底部导航RadioButton菜单栏后，改变RadioButton图标和文字变成蓝色,
     * 同时加载对应的Fragment页面内容
     *
     * @param index RadioButton在RadioGroup中的序号
     */
    private void setSelection(int index) {
        resetImg();
        //创建FragmentTransaction
        ft = getSupportFragmentManager().beginTransaction();
        //隐藏所有Fragment
        hideFragments(ft);
        //根据选择底部导航栏按钮切换加载对应内容的Fragment页面
        switch (index) {
            case 0:
                homePageFragmentLoad();
                break;
            case 1:
                musicFragmentLoad();
                break;
            case 2:
                foundFragmentLoad();
                break;
            case 3:
                myFragmentLoad();
                break;
        }
    }

    //设置点击RadioButton文字和背景颜色并加载MyFragment
    private void myFragmentLoad() {
        rb_my.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_my_on, 0, 0);
        rb_my.setTextColor(getColor(R.color.lightblue));
        if (mMyFragment == null) {
            mMyFragment = new MyFragment();
            ft.add(R.id.fg_content, mMyFragment);
        }
        ft.show(mMyFragment);
    }
    //设置点击RadioButton文字和背景颜色并加载FoundFragment
    private void foundFragmentLoad() {
        rb_found.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_found_on, 0, 0);
        rb_found.setTextColor(getColor(R.color.lightblue));
        if (mFoundFragment == null) {
            mFoundFragment = new FoundFragment();
            ft.add(R.id.fg_content, mFoundFragment);
        }
        ft.show(mFoundFragment);
    }
    //设置点击RadioButton文字和背景颜色并加载MusicFragment
    private void musicFragmentLoad() {
        rb_music.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_music_on, 0, 0);
        rb_music.setTextColor(getColor(R.color.lightblue));
        if (mMusicFragment == null) {
            mMusicFragment = new MusicFragment();
            ft.add(R.id.fg_content, mMusicFragment);
        }
        ft.show(mMusicFragment);
    }
    //设置点击RadioButton文字和背景颜色并加载HomePageFragment
    private void homePageFragmentLoad() {
        //变换图标
        rb_homePage.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_homepage_on, 0, 0);
        //变换文字颜色
        rb_homePage.setTextColor(getColor(R.color.lightblue));
        if (mHomePageFragment == null) {
            mHomePageFragment = new HomePageFragment();
            ft.add(R.id.fg_content, mHomePageFragment);
        }
        ft.show(mHomePageFragment);
    }

    /**
     * 隐藏所有Fragment
     */
    private void hideFragments(FragmentTransaction ft) {
        if (mHomePageFragment != null) {
            ft.hide(mHomePageFragment);
        }
        if (mMusicFragment != null) {
            ft.hide(mMusicFragment);
        }
        if (mFoundFragment != null) {
            ft.hide(mFoundFragment);
        }
        if (mMyFragment != null) {
            ft.hide(mMyFragment);
        }
        ft.commit();
    }

    /**
     * 恢复所有RadioButton默认颜色和图片
     */
    private void resetImg() {
        rb_homePage.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_homepage, 0, 0);
        rb_homePage.setTextColor(getColor(R.color.tab_text_bg));

        rb_music.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_music, 0, 0);
        rb_music.setTextColor(getColor(R.color.tab_text_bg));

        rb_found.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_found, 0, 0);
        rb_found.setTextColor(getColor(R.color.tab_text_bg));

        rb_my.setCompoundDrawablesWithIntrinsicBounds(0, R.mipmap.guide_my, 0, 0);
        rb_my.setTextColor(getColor(R.color.tab_text_bg));
    }



    //选择主页页面
    public void onHomePageClicked(View view) {
        setSelection(0);
    }

    //选择音乐页面
    public void onOrderClicked(View view) {
        setSelection(1);
    }

    //选择发现页面
    public void onFoundClicked(View view) {
        setSelection(2);
    }

    //选择我的页面
    public void onMyClicked(View view) {
        setSelection(3);
    }
}